package com.webserver.core;

import com.webserver.controller.ArticleController;
import com.webserver.controller.ToolsController;
import com.webserver.controller.UserController;
import com.webserver.http.HttpContext;
import com.webserver.http.HttpServletRequest;
import com.webserver.http.HttpServletResponse;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
 * 该类用于完成请求的处理工作
 */
public class DispatcherServlet {
    /*
    单例模式 java23种设计模式之一
    当一个类仅需要一个实例时，可使用该模式
    1；提供静态的私有的当前类的属性
    2：私有化构造方法
    3：提供静态的公开的获取当前实例的方法
    在方法中可判断步骤一的静态属性是否为null，是，则实例化对象
    否则直接返回该实例
     */
    private static DispatcherServlet obj;
    private DispatcherServlet(){

    }
    public static DispatcherServlet getInstance(){
        if (obj==null){
           obj = new DispatcherServlet();
        }
        return obj;
    }
    public void service(HttpServletRequest request, HttpServletResponse response){
        String path = request.getRequestURI();
        if ("/myweb/reg".equals(path)){
            //处理用户注册
            System.out.println("!!!!!!!!!!!!!！处理注册");
            UserController controller = new UserController();
            controller.reg(request,response);
        }else if ("/myweb/login".equals(path)){
            System.out.println("!!!!!!!1!!处理登录");
            UserController controller = new UserController();
            controller.login(request,response);
        }else if ("/myweb/showAllUser".equals(path)){
            UserController controller = new UserController();
            controller.showAllUser(request,response);
        }else if ("/myweb/writeArticle".equals(path)){
            System.out.println("处理文章！！！！！！");
            ArticleController articleController = new ArticleController();
            articleController.writeArticle(request,response);
        }else if ("/myweb/showAllArticle".equals(path)){
            ArticleController articleController = new ArticleController();
            articleController.showAllArticle(request,response);
        }else if("/myweb/createQR".equals(path)){
            ToolsController controller = new ToolsController();
            controller.createQr(request, response);
        }else {
            //将./webapps/myweb/index.html响应给浏览器
            File file = new File("./webbapps" + path);
            if (file.isFile()) {//如果定位是文件
                response.setEntity(file);
                //自己写的
//            int index = file.getName().indexOf('.');
//            String line = file.getName().substring(index+1,file.getName().length());
//           String value = mineMapping .get(line);
                //添加两个响应头 Content-Type Content-Length
            } else {//不是文件要么不存在要么是目录404情况
                file = new File("./webbapps/root/505.html");
                response.setStatusCode(404);
                response.setStatusReason("NotFund");
                response.setEntity(file);
            }
        }


            response.addHeader("Server", "WebServer");


    }

}
